<template>
  <van-button
    v-if="isfollow"
    class="follow-btn"
    round
    size="small"
    @click="onFlow"
    :loading="followloading"
  >已关注</van-button>
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    @click="onFlow"
    :loading="followloading"
  >关注</van-button>
</template>

<script>
import {addFollow, deleteFollow} from "@/api/user";

export default {
  name: "follow-articcle",
  props:{
    isfollow:{
      type:Boolean,
      required:true
    },
    articlesId:{
      type:[String,Number,Object],
      required: true
    }

  },
  data(){
    return{
      followloading:false

    }
  },
  methods:{

    //关注，取消关注
    async onFlow(){
      this.followloading=true
      try {

        if (this.isfollow){
          //如果关注了，点击了就是取消关注
          const { data } = await deleteFollow(this.articlesId)

          console.log(data)

        }else {

          const { data } = await addFollow(this.articlesId)
          console.log(data)
        }
        this.followloading=false
      this.$emit('update-isFollow',!this.isfollow)

      }catch (err){
        let messages='操作失败，请重试!'
        if (err.response && err.response.status===404){
          messages='你不能关注你自己'
        }
        this.followloading=false
        this.$toast(messages)
      }


    }
  }
}
</script>

<style scoped>

</style>
